package com.gzx.plugin.tjzy.feign.htCallRecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gzx.plugin.tjzy.feign.htCallRecord.mapper.GzxHtCallRecordFeignMapper;
import com.gzx.plugin.tjzy.modular.call.ht.entity.GzxHtCallRecord;
import com.gzx.plugin.tjzy.modular.call.ht.entity.bo.GzxHtCallRerordBo;
import com.gzx.plugin.tjzy.feign.htCallRecord.service.GzxHtCallRecordFeignService;
import com.gzx.tjzy.feign.resp.GzxHtCallRecordDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zjt
 * @description
 * @date
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GzxHtCallRecordServiceFeignImpl implements GzxHtCallRecordFeignService {

    private final GzxHtCallRecordFeignMapper gzxHtCallRecordMapper;

    @Override
    @DS("tjzy-async")
    public List<GzxHtCallRecordDto> getYcHtCallRecord(GzxHtCallRerordBo bo) {
        try{
            LambdaQueryWrapper<GzxHtCallRecord> wrapper = new LambdaQueryWrapper<>();
            wrapper.gt(GzxHtCallRecord::getCreatedTime, bo.getTodayStr());
            wrapper.in(GzxHtCallRecord::getUserName, bo.getUserName());
            wrapper.gt(GzxHtCallRecord::getId, bo.getId());
            wrapper.orderByAsc(GzxHtCallRecord::getId);
            wrapper.last("LIMIT 500");
            List<GzxHtCallRecord> gzxHtCallRecords = gzxHtCallRecordMapper.selectList(wrapper);
            ArrayList<GzxHtCallRecordDto> result = new ArrayList<>();
            for (int i = 0; i < gzxHtCallRecords.size(); i++) {
                GzxHtCallRecordDto dto = new GzxHtCallRecordDto();
                BeanUtil.copyProperties(gzxHtCallRecords.get(i),dto);
                result.add(dto);
            }
            return result;
        }catch (Exception e){
            log.error("同步银川数据发生异常了",e);
            throw e;
        }

    }

    @Override
    @DS("tjzy-async")
    public List<GzxHtCallRecordDto> getHtRecordInfo(List<String> callIds) {
        LambdaQueryWrapper<GzxHtCallRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(GzxHtCallRecord::getCallId, callIds);
        List<GzxHtCallRecord> gzxHtCallRecords = gzxHtCallRecordMapper.selectList(wrapper);
        ArrayList<GzxHtCallRecordDto> gzxHtCallRecordDtos = new ArrayList<>();
        for (GzxHtCallRecord gzxHtCallRecord : gzxHtCallRecords) {
            GzxHtCallRecordDto entity = new GzxHtCallRecordDto();
            BeanUtil.copyProperties(gzxHtCallRecord,entity);
            gzxHtCallRecordDtos.add(entity);
        }
        return gzxHtCallRecordDtos;
    }
}
